package com.itheima.service.impl;

import cn.itheima.health.resources.DateUtils;
import cn.itheima.health.resources.SMSUtils;
import cn.itheima.health.service.OrderService;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Order add(Map map) {
        //  1.查询当前日期  是否已经预约满   select count(1)  from t_ordersetting where  number>ressxxx  and  orderdate = xxxx
        long count = orderSettingMapper.isOrderSettingOk(DateUtils.parseString2Date((String)map.get("orderDate"),"yyyy-MM-dd"));
        Order  order = new Order();
      if(count>0){
           //  可以预约     新会员和老会员的问题
           String  telephone  = (String) map.get("telephone");
          Member member = memberMapper.findMemberByTelephone(telephone);
          if(member==null){
              //  新会员   会员表添加一条记录 + 生成订单
              member = new Member();
              member.setIdCard((String)map.get("idCard"));
              member.setSex((String)map.get("sex"));
              member.setName((String)map.get("name"));
              member.setPhoneNumber(telephone);
              Date reDate = new Date(System.currentTimeMillis());
              member.setRegTime(reDate);
              memberMapper.add(member); //  新用户添加一定要主键回显
              //  新用户生成订单
              order  = generateOrder(order,member,map);
          }else {
              //  老会员   当天有无预约过此套餐
              long  isOrder = orderMapper.findOrderIsExistByDateAndSetmealIdAndmemberId(
                      member.getId(),
                      DateUtils.parseString2Date((String)map.get("orderDate"),"yyyy-MM-dd"),
                      Integer.parseInt((String)map.get("setmealId")));
              //  根据老用户id  日期    套餐id  查询老用户当天有无预约过此套餐
         //  select  count(1)  from t_order  where  memberId= ?  and  orderdate = ?  and  setmeal_id = ?
               if(isOrder>0){
                   //  老会员已经预约
                   throw  new RuntimeException("请勿重新预约");
               }
               //  老会员可以预约  生成订单
             order =    generateOrder(order,member,map);
          }
      }else{
          // 不可以预约
          throw  new RuntimeException("当天预约已满");
      }
      return  order;
    }

    @Override
    public Map findOrderInfoByOrderId(Integer id) {
        return  orderMapper.findOrderInfoByOrderId(id);
    }

    /**
     *   订单的添加 封装
     * @param order
     * @param member
     * @param map
     */

    public  Order  generateOrder(Order order ,Member member,Map map){
        order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
        order.setOrderDate(DateUtils.parseString2Date((String)map.get("orderDate"),"yyyy-MM-dd"));
        order.setOrderStatus("未到诊");
        order.setOrderType((String)map.get("orderType"));
        order.setMemberId(member.getId());
        orderMapper.add(order);
        //   预约表  已经预约数+1
        orderSettingMapper.updateReservationsByOrderDate(DateUtils.parseString2Date((String)map.get("orderDate"),"yyyy-MM-dd"));

         //  发送短信给用户  预约成功！
//        SMSUtils.sendXXX

        return  order;

    }
}
